package com.aplink.generic.widgets;

import java.text.ParseException;
import java.util.Calendar;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.MasterConfigContant;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.google.datetimepicker.date.DatePickerDialog;
import com.aplink.generic.google.datetimepicker.date.DatePickerDialog.OnDateSetListener;
import com.aplink.generic.google.datetimepicker.time.RadialPickerLayout;
import com.aplink.generic.google.datetimepicker.time.TimePickerDialog;
import com.aplink.generic.google.datetimepicker.time.TimePickerDialog.OnTimeSetListener;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.ObjPropInfo;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.DateTimePickerSpec;
import com.aplink.generic.util.DateTimeUtils;

public class ApLinkDateTimePicker extends ApLinkBaseView implements
        OnTimeSetListener, OnDateSetListener {
	private Calendar mCalendar = null;
	private final DateTimePickerSpec mDateTimePickerSpec;

	private ApLinkBaseView mDateValueBaseView;

	private View mDateValueView;
	private final View.OnClickListener mOnDatePickerListener = new OnClickListener() {

		@Override
		public void onClick(final View v) {
			final DatePickerDialog datePickerDialog = DatePickerDialog
			        .newInstance(ApLinkDateTimePicker.this,
			                mCalendar.get(Calendar.YEAR),
			                mCalendar.get(Calendar.MONTH),
			                mCalendar.get(Calendar.DATE));
			datePickerDialog
			        .show(getChildFragmentManager(), "DatePickerDialog");
		}
	};
	private final View.OnClickListener mOnTimePickerListener = new OnClickListener() {

		@Override
		public void onClick(final View v) {
			final TimePickerDialog timePickerDialog = TimePickerDialog
			        .newInstance(ApLinkDateTimePicker.this,
			                mCalendar.get(Calendar.HOUR),
			                mCalendar.get(Calendar.SECOND),
			                mDateTimePickerSpec.isIs24Hour());
			timePickerDialog
			        .show(getChildFragmentManager(), "TimePickerDialog");
		}
	};
	private ApLinkBaseView mTimeValueBaseView;

	private View mTimeValueView;

	public ApLinkDateTimePicker(final GenericFragment genericFragment,
	        final ControlSpec controlSpec, final View view, Bundle bundle) {
		super(genericFragment, controlSpec, view, bundle);
		mDateTimePickerSpec = (DateTimePickerSpec) controlSpec;
		mCalendar = Calendar.getInstance();
	}

	@Override
	public void clear() {
		mCalendar = Calendar.getInstance();
	}

	@Override
	public GenericAttribute getAttribute() {
		final String name = this.mDateTimePickerSpec.getFullColumnName();
		final ObjPropInfo objPropInfo = getGenericFragment().getContentData()
		        .get(name);
		final String value = String
		        .valueOf(mCalendar.getTimeInMillis() / 1000L);
		return GenericAttribute.obtain(value, objPropInfo);
	}

	@Override
	public GenericDictionary<String, GenericAttribute> getListAttributes() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public View getView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void initViewProperties() {
		if (!TextUtils.isEmpty(mDateTimePickerSpec.getTimeValueControl())) {
			mTimeValueBaseView = getGenericFragment().getAPLinkViewByID(
			        mDateTimePickerSpec.getTimeValueControl());
			if (mTimeValueBaseView == null) {
				mTimeValueView = getGenericFragment().findViewByID(
				        mDateTimePickerSpec.getTimeValueControl());
			} else {
				mTimeValueView = mTimeValueBaseView.getView();
			}
		}

		if (!TextUtils.isEmpty(mDateTimePickerSpec.getDateValueControl())) {
			mDateValueBaseView = getGenericFragment().getAPLinkViewByID(
			        mDateTimePickerSpec.getDateValueControl());
			if (mDateValueBaseView == null) {
				mDateValueView = getGenericFragment().findViewByID(
				        mDateTimePickerSpec.getDateValueControl());
			} else {
				mDateValueView = mDateValueBaseView.getView();
			}
		}
	}

	@Override
	public void languageChanged() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void loadBindingObject() {
		String dateTimeValue = getPassedObject().getValue(
		        mDateTimePickerSpec.getFullColumnName());
		if (!TextUtils.isEmpty(dateTimeValue)
		        && TextUtils.isDigitsOnly(dateTimeValue)) {
			long value = Long.parseLong(dateTimeValue);
			mCalendar.setTimeInMillis(value * 1000L);
			try {
				setDate();
				setTime();
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void loadDefaultObject() {

	}

	@Override
	public void onDateSet(final DatePickerDialog dialog, final int year,
	        final int monthOfYear, final int dayOfMonth) {
		try {
			mCalendar.set(year, monthOfYear, dayOfMonth,
			        mCalendar.get(Calendar.HOUR),
			        mCalendar.get(Calendar.SECOND));
			setDate();
		} catch (final ParseException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onTimeSet(final RadialPickerLayout view, final int hourOfDay,
	        final int minute) {
		try {
			mCalendar.set(mCalendar.get(Calendar.YEAR),
			        mCalendar.get(Calendar.MONTH),
			        mCalendar.get(Calendar.DATE), hourOfDay, minute);
			setTime();
		} catch (final ParseException e) {
			e.printStackTrace();
		}
	}

	private void setDate() throws ParseException {
		final String value = DateTimeUtils.getDateTime(true,
		        mCalendar.getTimeInMillis() / 1000L,
		        MasterConfigContant.DATE_FORMAT);
		mDateValueView = getGenericFragment().findViewByID(
		        mDateTimePickerSpec.getDateValueControl());
		if (mDateValueView != null) {
			if (mDateValueView instanceof TextView) {
				((TextView) mDateValueView).setText(value);
			}
		}
	}

	@Override
	protected void setEventListener() {
		if (!TextUtils.isEmpty(mDateTimePickerSpec.getDateValueControl())) {
			if (mDateValueBaseView != null) {
				mDateValueBaseView.setOnClickListener(mOnDatePickerListener);
			} else if (getGenericFragment().getFormDesign()
			        .getControlSpecCollection()
			        .get(this.mDateTimePickerSpec.getDateValueControl()) != null) {
				getGenericFragment().getFormDesign().getControlSpecCollection()
				        .get(this.mDateTimePickerSpec.getDateValueControl())
				        .setOnClickListener(mOnDatePickerListener);
			} else if (mDateValueView != null) {
				mDateValueView.setOnClickListener(mOnDatePickerListener);
			}
		}

		if (!TextUtils.isEmpty(mDateTimePickerSpec.getTimeValueControl())) {
			if (mTimeValueBaseView != null) {
				mTimeValueBaseView.setOnClickListener(mOnTimePickerListener);
			} else if (getGenericFragment().getFormDesign()
			        .getControlSpecCollection()
			        .get(this.mDateTimePickerSpec.getTimeValueControl()) != null) {
				getGenericFragment().getFormDesign().getControlSpecCollection()
				        .get(this.mDateTimePickerSpec.getTimeValueControl())
				        .setOnClickListener(mOnTimePickerListener);
			} else if (mDateValueView != null) {
				mTimeValueView.setOnClickListener(mOnTimePickerListener);
			}
		}
	}

	@Override
	public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setOnClickListener(final OnClickListener onClickListener) {
		// TODO Auto-generated method stub

	}

	private void setTime() throws ParseException {
		final String value = DateTimeUtils.getDateTime(true,
		        mCalendar.getTimeInMillis() / 1000L,
		        MasterConfigContant.TIME_FORMAT);
		mTimeValueView = getGenericFragment().findViewByID(
		        mDateTimePickerSpec.getTimeValueControl());
		if (mTimeValueView != null) {
			if (mTimeValueView instanceof TextView) {
				((TextView) mTimeValueView).setText(value);
			}
		}
	}
}
